package com.zhijian.medical.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.entity.dto.PersonDto;
import com.zhijian.medical.entity.po.Device;
import com.zhijian.medical.entity.vo.request.RoutinePatientSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.response.DeviceTsVo;
import com.zhijian.medical.entity.vo.response.DeviceVo;
import com.zhijian.medical.holder.CurrentUserHolder;
import com.zhijian.medical.service.PatientDeviceService;
import com.zhijian.medical.util.AssertUtil;
import com.zhijian.response.annotation.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Slf4j
@Controller
@Api(tags = "常规病理-设备相关处理")
public class RoutinePatientDeviceController {

    @Autowired
    PatientDeviceService patientDeviceService;

    @Response
    @GetMapping("/routine/patient/device")
    @ApiOperation("根据设备编码查询设备")
    public DeviceTsVo selectDeviceByCode(String deviceCode, String typeStr){
        List<DeviceTsVo> result = patientDeviceService.selectDeviceByDevice(deviceCode, typeStr);
        AssertUtil.check(CollectionUtil.isEmpty(result), "设备没有找到！");
        return result.get(0);
    }

    @Response
    @GetMapping("/routine/patient/device/type")
    @ApiOperation("根据设备类型查询设备")
    public List<DeviceTsVo> selectDeviceByType(String typeStr){
        return patientDeviceService.selectDeviceByDevice(null, typeStr);
    }

    @Response
    @PutMapping("/routine/patient/device")
    @ApiOperation("设备状态变更")
    public void changeState(@RequestBody RoutinePatientSaveOrUpdateVo.DeviceChangeEntity deviceChangeEntity){
        patientDeviceService.changeState(deviceChangeEntity);
    }

    @Response
    @PutMapping("/routine/patient/device/adjust")
    @ApiOperation("修改校准时间")
    public void updateAdjustDate(@RequestBody RoutinePatientSaveOrUpdateVo.DeviceChangeEntity deviceChangeEntity){
        PersonDto personDto = CurrentUserHolder.getUser();
        patientDeviceService.adjustDate(personDto, deviceChangeEntity);
    }
}
